/* This file is part of MyRename
 * Copyright (C) 2005-2009 Kai-Chieh Ku (kjackie@gmail.com)
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either
 * version 3 of the License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.	See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
 */

// XRichEditCtrl.cpp : implementation file
//

#include "stdafx.h"
#include "MyRename.h"
#include "MyRenameDlg.h"
#include "language.h"
#include "others.h"

#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif

CFont CXEditCtrl::m_fontSymbol;
CMenu CXEditCtrl::m_btnMenu;

// CXEditCtrl

IMPLEMENT_DYNAMIC(CXEditCtrl, CEdit)
CXEditCtrl::CXEditCtrl()
	: m_xEditStyle(0), m_pButton(NULL)
{

	if (!m_btnMenu.m_hMenu) {
		m_btnMenu.CreatePopupMenu();
		m_btnMenu.AppendMenu(MF_STRING, IDR_ADDVAR_NAME, GetResString(IDR_ADDVAR_NAME));
		m_btnMenu.AppendMenu(MF_STRING, IDR_ADDVAR_TITLE, GetResString(IDR_ADDVAR_TITLE));
		m_btnMenu.AppendMenu(MF_STRING, IDR_ADDVAR_EXT, GetResString(IDR_ADDVAR_EXT));
		m_btnMenu.AppendMenu(MF_STRING, IDR_ADDVAR_NUM, GetResString(IDR_ADDVAR_NUM));
	}
	if (!m_fontSymbol.m_hObject) //create 11pt symbol font
		m_fontSymbol.CreatePointFont(110, _T("Marlett"));
}

CXEditCtrl::~CXEditCtrl()
{
	if (m_pButton) {
		m_pButton->DestroyWindow();
		delete m_pButton;
	}
}

BEGIN_MESSAGE_MAP(CXEditCtrl, CEdit)
	//{{AFX_MSG_MAP(CXEditCtrl)
	ON_CONTROL_REFLECT(EN_SETFOCUS, OnEnSetFocus)
	ON_CONTROL_REFLECT(EN_KILLFOCUS, OnEnKillFocus)
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

void CXEditCtrl::ModifyXECStyle(DWORD dwAdd, DWORD dwRemove/* = XEC_NONE*/)
{
	ASSERT((dwAdd & dwRemove) == 0);

	if (!(m_xEditStyle & XEC_RIGHTBTN) && (dwAdd & XEC_RIGHTBTN)) { //add right button
		CRect rcWin, rc;
		CWnd *pParent;
		GetWindowRect(rcWin);
		pParent = GetParent();
		if (pParent) {
			pParent->ScreenToClient(rcWin);
			rc = rcWin;
			rc.left = rc.right - 20;
			rcWin.right -= 25;
			MoveWindow(rcWin);
			ASSERT(m_pButton == NULL);
			m_pButton = new CButton;
			m_pButton->Create(_T("<"), WS_CHILD | WS_VISIBLE | WS_TABSTOP | BS_VCENTER | BS_CENTER, rc, pParent, GetDlgCtrlID());
			m_pButton->EnableWindow(IsWindowEnabled());
			if (m_fontSymbol.m_hObject) {
				m_pButton->SetFont(&m_fontSymbol);
				m_pButton->SetWindowText(_T("w")); //set left arrow
			}
		}
		else
			dwAdd &= (~XEC_RIGHTBTN);
	}
	if ((m_xEditStyle & XEC_RIGHTBTN) && (dwRemove & XEC_RIGHTBTN)) { // remove right button
		ASSERT(m_pButton != NULL);
		m_pButton->DestroyWindow();
		delete m_pButton;
		m_pButton = NULL;
		CRect rcWin;
		GetWindowRect(rcWin);
		GetParent()->ScreenToClient(rcWin);
		rcWin.right += 25;
		MoveWindow(rcWin);
	}

	m_xEditStyle |= dwAdd;
	m_xEditStyle &= (~dwRemove);
}

void CXEditCtrl::SetDefaultText(LPCTSTR sDefText)
{
	if (sDefText)
		m_sDefaultText = sDefText;
}

void CXEditCtrl::SetMinMax(int iMin, int iMax)
{
	ASSERT(iMin < iMax);
	m_iMin = iMin;
	m_iMax = iMax;
}

void CXEditCtrl::AppendFormatText(LPCTSTR sFormat, ...)
{
	CString str;
	GetWindowText(str);
	va_list argv;
	va_start(argv, sFormat);
	str.AppendFormatV(sFormat, argv);
	va_end(argv);
	SetWindowText(str);
}

void CXEditCtrl::Localize()
{
	if (m_btnMenu) {
		m_btnMenu.ModifyMenu(IDR_ADDVAR_NAME, MF_BYCOMMAND | MF_STRING, IDR_ADDVAR_NAME, GetResString(IDR_ADDVAR_NAME));
		m_btnMenu.ModifyMenu(IDR_ADDVAR_TITLE, MF_BYCOMMAND | MF_STRING, IDR_ADDVAR_TITLE, GetResString(IDR_ADDVAR_TITLE));
		m_btnMenu.ModifyMenu(IDR_ADDVAR_EXT, MF_BYCOMMAND | MF_STRING, IDR_ADDVAR_EXT, GetResString(IDR_ADDVAR_EXT));
		m_btnMenu.ModifyMenu(IDR_ADDVAR_NUM, MF_BYCOMMAND | MF_STRING, IDR_ADDVAR_NUM, GetResString(IDR_ADDVAR_NUM));
	}
}

// CXEditCtrl message handlers

void CXEditCtrl::OnEnSetFocus()
{
}

void CXEditCtrl::OnEnKillFocus()
{
	CString str;
	GetWindowText(str);

	if (str.IsEmpty())
		SetWindowText(m_sDefaultText);
	else if (m_xEditStyle & XEC_MINMAX) {
		if (m_xEditStyle & XEC_INTEGER) {
			int iCurrent = _ttol(str);
			if (iCurrent > m_iMax || iCurrent < m_iMin || !isInteger(str)) {
				if (m_sDefaultText.IsEmpty())
					m_sDefaultText.Format(_T("%d"), m_iMin);
				SetWindowText(m_sDefaultText);
			}
		}
	}
	else {
		if (m_xEditStyle & XEC_INTEGER) {
			if (!isInteger(str)) {
				if (m_sDefaultText.IsEmpty())
					m_sDefaultText.Format(_T("%d"), 0);
				SetWindowText(m_sDefaultText);
			}
		}
	}
}

BOOL CXEditCtrl::OnCommand(WPARAM wParam, LPARAM lParam)
{
	switch (wParam >> (sizeof(WPARAM) << 2)) {
		case 0:
			switch (wParam & 0x0000FFFF) {
				case IDR_ADDVAR_NAME:
					ReplaceSel(_T(":name"), TRUE);
					return TRUE;
				case IDR_ADDVAR_TITLE:
					ReplaceSel(_T(":title"), TRUE);
					return TRUE;
				case IDR_ADDVAR_EXT:
					ReplaceSel(_T(":ext"), TRUE);
					return TRUE;
				case IDR_ADDVAR_NUM:
					ReplaceSel(_T(":num"), TRUE);
					return TRUE;
				default:
					{
						CRect rc;
						m_pButton->GetWindowRect(rc);
						m_btnMenu.TrackPopupMenu(TPM_LEFTALIGN | TPM_BOTTOMALIGN | TPM_RIGHTBUTTON, rc.right, rc.bottom, this);
					}
					return TRUE;
			}
			break;
	}

	return CEdit::OnCommand(wParam, lParam);
}

LRESULT CXEditCtrl::DefWindowProc(UINT message, WPARAM wParam, LPARAM lParam)
{
	switch (message) {
		case WM_ENABLE:
			if (m_pButton)
				m_pButton->EnableWindow((BOOL) wParam);
			break;
	}

	return CEdit::DefWindowProc(message, wParam, lParam);
}
